//
//  User+Create.m
//  eyr
//
//  Created by Tor Gjøsæter on 4/25/12.
//  Copyright (c) 2012 University of Bergen. All rights reserved.
//

#import "User+Create.h"

@implementation User (Create)

+ (User *)createUser:(NSString *) name
inManagedObjectContext:(NSManagedObjectContext * ) context{
  
    
    
    
    User *user = nil;
    
    NSFetchRequest *request  = [NSFetchRequest fetchRequestWithEntityName:@"User"];
    request.predicate = [NSPredicate predicateWithFormat:@"name = %@", name];
    
    NSError *error;
    NSArray *matches = [context executeFetchRequest:request error:&error];
    
    if([matches count] > 1){
        //Bruker finnes allereie
        //Handle error
    }else if ([matches count] == 0)
    {
        //Adding our default user
        user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:context];
        user.name = name;
//        [user addGraphsObject:[Graph createRandomGraph:context]];
    }
    else{
        user = [matches lastObject];
    }
    return user;

}


@end
